﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBenhVien
{
    public partial class frmTiepNhanBenh : Form
    {
        float tienkham = 60000;
        public frmTiepNhanBenh()
        {
            InitializeComponent();
            if (frmManHinhChinh.current_user.usertype == DBOUser.User_Type.TiepTan)
            {
                grpThuNgan.Visible = false;
            }
            else if (frmManHinhChinh.current_user.usertype == DBOUser.User_Type.ThuNgan)
            {
                grpTiepNhan.Visible = false;
                btnBenhNhan.Visible = false;
            }
            LoadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBenhNhan_Click(object sender, EventArgs e)
        {
            frmDanhSachBenh ds = new frmDanhSachBenh();
            if (ds.ShowDialog() == DialogResult.OK)
            {
                btThem.Enabled = true;
                txtID.Text = ds.ret.id.ToString();
                txtHoten.Text = ds.ret.hoten;
                txtNgaysinh.Text = ds.ret.ngaysinh.ToShortDateString();
            }
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            if (txtID.Text.Length > 0)
            {                
                DBODanhSachKham o = new DBODanhSachKham();
                o.benhnhan_id = int.Parse(txtID.Text);
                o.ngaykham = DateTime.Now;
                o.nhanvien_id = frmManHinhChinh.current_user.id;
                o.Them();
                LoadData();

                txtID.ResetText();
                txtHoten.ResetText();
                txtNgaysinh.ResetText();
                btThem.Enabled = false;
            }
            else
            {
                MessageBox.Show("Chưa chọn bệnh nhân");
            }
        }

        private void LoadData()
        {
            DataTable dt = DAODanhSachKham.LoadAll(DateTime.Now);
            dg.DataSource = dt;
            if (dt.Rows.Count > 0)
            {
                dg.Columns["nhanvien_id"].Visible = false;
                dg.Columns["benhnhan_id"].Visible = false;
            }
        }
        
        private void btnDongTien_Click(object sender, EventArgs e)
        {
            if (dg.SelectedRows.Count > 0)
            {
                if ((int)dg.CurrentRow.Cells["sothutu"].Value == 0)
                {
                    DBODanhSachKham o = new DBODanhSachKham();
                    o.id = (int)dg.CurrentRow.Cells["id"].Value;
                    o.CapNhatSoThuTu();
                    DBOHoaDon h = new DBOHoaDon { 
                        benhnhan_id = (int)dg.CurrentRow.Cells["benhnhan_id"].Value,
                        dichvu_id = 0,
                        ngaythanhtoan = DateTime.Now,
                        nhanvien_id = (int)dg.CurrentRow.Cells["nhanvien_id"].Value,
                        tongtien = tienkham                        
                    };
                    h.add();
                    LoadData();
                }
                else
                {
                    MessageBox.Show("Bệnh nhân đã đóng tiền.");
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            int sothutu = (int)dg.CurrentRow.Cells["sothutu"].Value;
            PrintHoaDonKhamBenh p = new PrintHoaDonKhamBenh(sothutu);
            p.ShowDialog();            
        }   
    }
}
